import hashlib
import time

def calculate_hash(text, nonce):
    input_data = f"{text}{nonce}"
    sha256 = hashlib.sha256()
    sha256.update(input_data.encode('utf-8'))
    return sha256.hexdigest()

def mine_pow(text, difficulty):
    nonce = 0
    start_time = time.time()
    
    while True:
        hash_result = calculate_hash(text, nonce)
        if hash_result.startswith('0' * difficulty):
            end_time = time.time()
            mining_time = end_time - start_time
            return nonce, hash_result, mining_time
        nonce += 1

def main():
    text = input("请输入要进行挖矿的文本：")
    if not text:
        text = "ShenZhenTechnologyUniversity_BlockChain"
    
    for difficulty in [4, 5, 6]:
        print(f"\n开始进行难度为{difficulty}的挖矿...")
        nonce, hash_result, mining_time = mine_pow(text, difficulty)
        print(f"找到满足条件的nonce值：{nonce}")
        print(f"对应的哈希值：{hash_result}")
        print(f"耗时：{mining_time:.2f}秒")

if __name__ == "__main__":
    main()